
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var api = require('../../utils/api.js');
var qqmapsdk;
Page({

  data: {
    city:'',
    cityList: [],
    cityList1:[]
  },

  // 搜索栏
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    let that=this
    that.setData({
      inputVal: e.detail.value
    });
    let cityList = that.data.cityList
    let cityList1 = that.data.cityList1
    cityList = cityList1
    that.setData({
      cityList
    })
  },

  setSearchStorage:function(){
    let that=this
    let cityList=that.data.cityList
    let arr = []
    var inputVal = that.data.inputVal;
    //
    cityList.forEach(function (i) {
      var name = i.name;
      if (name.indexOf(inputVal) != -1) {
        arr.push({ 'name': name });
      }
    });
    if (inputVal == '') {
      arr = []
    }
    that.setData({
      cityList: arr
    })
    
      
  },
  onShow:function(){
      wx.hideTabBarRedDot({
        index: 3
      })
  },

  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'JB3BZ-OLZCG-NSTQ2-IY7AB-LXSG2-SUFZN'
    });
   let that=this
   that.setData({
     city: options.city
   });

    api.cityList({
      page: {
        size: 20,
        current: 0,
        isStartPage: false,
        pages: 1,
      }
    }).then(res => {
 
      if (res.result) {
        that.setData({
          cityList: res.object.records,
          cityList1: res.object.records
        })
      }
    })
  },

 tapCity:function(e){
   api.cityId({
     name: e.target.dataset.city
   }).then(res => {
     if (res.result) {
       wx.setStorageSync("cityId", res.object.id)
     }
   })
   setTimeout(function(){
     wx.reLaunch({
       url: '../index/index?city=' + e.target.dataset.city,
     })
   },1000)
    
  },

  getUserLocation: function (e) {
    wx.reLaunch({
      url: '../index/index',
    })
  }

})